<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Dialog Click Button Sequence Enter Password Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" type="text/css" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  .alink { text-decoration: underline; color: blue; cursor: pointer; }
  #area { height: 100px; display: block; }
  #dialog { height: auto !important; }
  .ui-dialog { font-size: 10px; }
  .ui-dialog .ui-dialog-titlebar { padding: 2px 3px; height: 15px; }
  .ui-button { outline: 0; }
  .ui-dialog .ui-dialog-content { padding: 0px; margin: 0 5px; }
  input[id="password"] { width: 120px; }
  input[id="verify"] { width: 120px; }
  /* 160 + 80 + 50 + 10 */
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/ui_utils.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000; // set episode interval to 2000 seconds

var resetUI = function(div){
  if(div.html().length > 0) $('#dialog').dialog('destroy');
  div.empty(); // clear previous problem, if any
}

var createDialog = function(div){
  var html = '<p>' + ui_utils.generateWords(4,8) + '</p>';
  div.append(html);

  $('#dialog').dialog({ height: 70, position: {my: 'center', at: 'center', of: document.getElementById('area')} });

  $('.ui-dialog')[0].style.margin = core.randi(-10,20) + 'px ' + core.randi(5,25) + 'px';
  $('.ui-dialog')[0].style.width = core.randi(90,120) + 'px';
  $('.ui-dialog')[0].style.height = core.randi(70,100) + 'px';
}

var genProblem = function() {
  var task_state = -1.0;
  // init: click dialog  
  var dialogDiv = $('#dialog');
  resetUI(dialogDiv);

  // main: click-dialog 
  var dialogDiv = $('#dialog'); 
  if (dialogDiv.length == 0)  d3.select('#area').append('div').attr('id', 'dialog');
  var dialogDiv = $('#dialog');

  createDialog(dialogDiv);

  // $('#query').html('Close the dialog box by clicking the "x".');
  $('button.ui-button').on('click', function(){ console.log('click-dialog'); task_state=1.0; });

  // click-button-sequence
  var button_state = -1.0
  var L = core.randi(0, 118); var U = core.randi(0, 30) + 200;
  d3.select('#subbtn1').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn1').on('click', function(){
    if (button_state == -1.0 && task_state == 1.0) {
      button_state = 1.0
    } else {
      button_state = -2.0
    }
    console.log('click-button-sequence: ONE', button_state);
  });

  var L = core.randi(0, 118); var U = core.randi(0, 30) + 200;
  d3.select('#subbtn2').attr('style', 'position:absolute; left:'+L+'px; top:'+U+'px');
  d3.select('#subbtn2').on('click', function(){
    if (button_state == 1.0 && task_state == 1.0) {
      button_state = 2.0
    } else {
      button_state = -2.0
    }
    console.log('click-button-sequence: TWO', button_state);
  });

  d3.select('#password')[0][0].value ='';
  d3.select('#verify')[0][0].value ='';

  var password = ui_utils.generateString(2,6);

  // reward awarder
  d3.select('#subbtn').on('click', function(){
    var p = d3.select('#password')[0][0].value;
    var v = d3.select('#verify')[0][0].value;
    var r = (p === password && v === password) ? 1.0 : -1.0;
    console.log('enter-password', r);
    if (button_state == 2.0 && task_state == 1.0) {
      core.endEpisode(r, r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });

  // generate query text in the UI
  d3.select('#query').html('Close the dialog box by clicking the "x", click button ONE, then click button TWO, and then enter the password "<span class="bold">' + password + '</span>" into both text fields and press submit.');
  // d3.select('#query').html('Click button ONE, then click button TWO, and enter the password "<span class="bold">' + password + '</span>" into both text fields and press submit, after closing the dialog box by clicking the "x",');

}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id='dialog'></div>
    <button id="subbtn1">ONE</button>
    <button id="subbtn2">TWO</button>
    <div id="form">
      <p><label>Password</label><input type="password" id="password"></p>
      <p><label>Verify password</label><input type="password" id="verify"></p>
      <button id="subbtn" class="secondary-action">Submit</button>
    </div>
  </div>
</div>
</body>
</html>
